<?php	require_once(dirname(__FILE__).'/inc/config.inc.php');IsModelPriv('site');

/*
**************************
(C)2010-2014 phpMyWind.com
update: 2014-5-30 17:39:07
person: Feng
**************************
*/


//初始化参数
$tbname = '#@__site';
$gourl  = 'site.php';
$action = isset($action) ? $action : '';
$webswitch = isset($webswitch) ? $webswitch : 'false';

//添加新站点
if($action == 'add')
{

	$r = $dosql->GetOne("SELECT `id` FROM `$tbname` WHERE `sitekey`='$site_key'");
	if(isset($r['id']))
	{
		Msg('该站点标识已存在！', '-1','iocn:0,time:800');
		exit();
	}

	$sql = "INSERT INTO `$tbname` (`sitename`, `sitekey`) VALUES ('$site_name', '$site_key')";
	if($dosql->ExecNoneQuery($sql))
	{

		$newsiteid = $dosql->GetLastID();
		$data_str = "INSERT INTO `#@__webconfig` (`siteid`, `varname`, `varinfo`, `vardesc`, `varcheck`, `vargroup`, `vartype`, `varvalue`, `checkinfo`, `orderid`) VALUES('$newsiteid','cfg_webname_$site_key','网站名称','','','0','string','$webname','true','1');
		INSERT INTO `#@__webconfig` (`siteid`, `varname`, `varinfo`, `vardesc`, `varcheck`, `vargroup`, `vartype`, `varvalue`, `checkinfo`, `orderid`) VALUES('$newsiteid','cfg_weburl_$site_key','网站地址','','','0','string','$weburl','true','2');
		INSERT INTO `#@__webconfig` (`siteid`, `varname`, `varinfo`, `vardesc`, `varcheck`, `vargroup`, `vartype`, `varvalue`, `checkinfo`, `orderid`) VALUES('$newsiteid','cfg_webpath_$site_key','网站目录','','','0','string','$webpath','true','3');
		INSERT INTO `#@__webconfig` (`siteid`, `varname`, `varinfo`, `vardesc`, `varcheck`, `vargroup`, `vartype`, `varvalue`, `checkinfo`, `orderid`) VALUES('$newsiteid','cfg_author_$site_key','网站作者','','','0','string','PHPMyWind Team','true','4');
		INSERT INTO `#@__webconfig` (`siteid`, `varname`, `varinfo`, `vardesc`, `varcheck`, `vargroup`, `vartype`, `varvalue`, `checkinfo`, `orderid`) VALUES('$newsiteid','cfg_generator_$site_key','程序引擎','','','0','string','PHPMyWind CMS','true','5');
		INSERT INTO `#@__webconfig` (`siteid`, `varname`, `varinfo`, `vardesc`, `varcheck`, `vargroup`, `vartype`, `varvalue`, `checkinfo`, `orderid`) VALUES('$newsiteid','cfg_keyword_$site_key','关键字设置','','','0','string','','true','6');
		INSERT INTO `#@__webconfig` (`siteid`, `varname`, `varinfo`, `vardesc`, `varcheck`, `vargroup`, `vartype`, `varvalue`, `checkinfo`, `orderid`) VALUES('$newsiteid','cfg_description_$site_key','网站描述','','','0','bstring','','true','7');
		INSERT INTO `#@__webconfig` (`siteid`, `varname`, `varinfo`, `vardesc`, `varcheck`, `vargroup`, `vartype`, `varvalue`, `checkinfo`, `orderid`) VALUES('$newsiteid','cfg_copyright_$site_key','版权信息','','','0','bstring','Copyright © 2010 - 2013 phpMyWind.com All Rights Reserved','true','8');
		INSERT INTO `#@__webconfig` (`siteid`, `varname`, `varinfo`, `vardesc`, `varcheck`, `vargroup`, `vartype`, `varvalue`, `checkinfo`, `orderid`) VALUES('$newsiteid','cfg_icp_$site_key','备案编号','','','0','string','','true','9');
		INSERT INTO `#@__webconfig` (`siteid`, `varname`, `varinfo`, `vardesc`, `varcheck`, `vargroup`, `vartype`, `varvalue`, `checkinfo`, `orderid`) VALUES('$newsiteid','cfg_webswitch_$site_key','启用站点','','','0','bool','$webswitch','true','10');
		INSERT INTO `#@__webconfig` (`siteid`, `varname`, `varinfo`, `vardesc`, `varcheck`, `vargroup`, `vartype`, `varvalue`, `checkinfo`, `orderid`) VALUES('$newsiteid','cfg_switchshow_$site_key','关闭说明','','','0','bstring','对不起，网站维护，请稍后登陆。<br />网站维护期间对您造成的不便，请谅解！','true','11');";
		$querys = explode(';', $data_str);
		foreach($querys as $sql)
		{
			if(trim($sql) == '') continue;
			$dosql->ExecNoneQuery($sql);
		}

	
		WriteConfig();
		echo Msg('添加完成','2','iocn:1,time:800');
		exit();
	}
}


//修改站点
else if($action == 'update')
{

	$sql = "UPDATE `$tbname` SET `sitename`='$site_name' WHERE `id`=$id";
	if($dosql->ExecNoneQuery($sql))
	{
		echo Msg('修改完成','2','icon:1,time:800');

		exit();
	}
}


//删除站点
else if($action == 'del')
{
	if($id == 1)
	{
		Msg('抱歉，不能删除默认站点！','-1','iocn:0');
		exit();
	}

	if($dosql->ExecNoneQuery("DELETE FROM `$tbname` WHERE `id`=$id"))
	{

		//设置区分站点的表名
		$tbnames = array('admanage','adtype','diymenu','infoclass','infolist','job',
						 'message','nav','vote','webconfig',
						 'weblink','weblinktype');

		//删除所有该站点信息
		foreach($tbnames as $tbn)
		{
			$dosql->ExecNoneQuery("DELETE FROM `#@__$tbn` WHERE `siteid`=$id");
		}
		
		
		//设置登录站点
		$r = $dosql->GetOne("SELECT `id`,`sitekey` FROM `#@__site` ORDER BY `id` ASC");
		if(isset($r['id']))
		{
			$_SESSION['siteid']  = $r['id'];
			$_SESSION['sitekey'] = $r['sitekey'];
		}
		else
		{
			$_SESSION['siteid']  = '';
			$_SESSION['sitekey'] = '';
		}


		WriteConfig();
    	echo Msg('删除成功','2','iocn:1,time:800');
		exit();
	}
}


//无条件返回
else
{
    header("location:$gourl");
	exit();
}


//更新配置函数
function WriteConfig()
{
	global $dosql;


	$str = '<?php	if(!defined(\'IN_PHPMYWIND\')) exit(\'Request Error!\');'."\r\n\r\n";
	$dosql->Execute("SELECT `varname`,`vartype`,`varvalue`,`vargroup` FROM `#@__webconfig` ORDER BY `orderid` ASC");
	while($row = $dosql->GetArray())
	{
		//统计代码转义
		if($row['varname'] == 'cfg_countcode')
		{
			$row['varvalue'] = stripslashes($row['varvalue']);
		}

		if($row['vartype'] == 'number')
		{
			if($row['varvalue'] == '')
			{
				$row['varvalue'] = 0;
			}

			$str .= "\${$row['varname']} = ".$row['varvalue'].";\r\n";
		}
		else
		{
			$str .= "\${$row['varname']} = '".str_replace("'",'',$row['varvalue'])."';\r\n";
		}
	}
	$str .= '?>';

	Writef(PHPMYWIND_INC.'/config.cache.php',$str);
}
?>